package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.app.AppContext;
import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.CarDetails;
import com.jzg.jcptadmin.data.vo.SuccessData;
import com.jzg.jcptadmin.viewinterface.OffOnLineTaskViewInterface;

import java.util.Map;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by jzg on 2016/4/1.
 * 线下详情
 */
public class OffOnLineTaskViewPresenter extends BasePresenter<OffOnLineTaskViewInterface> {

    private DataManager dataManager;
    private OffOnLineTaskViewInterface mView;
    private Subscription mSubscription;

    public OffOnLineTaskViewPresenter(DataManager dataManager) {
        this.dataManager = dataManager;
    }

    @Override
    public void attachView(OffOnLineTaskViewInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }

    public void loadTaskView() {
        if (!AppContext.isNetWork) return;
        Map<String, String> params = mView.getParams();
        if (!checkSubmitInfo(params)) return;
        mSubscription = dataManager.OffLineTaskViewData(params).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Observer<CarDetails>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if (e!=null&& mView!=null) {

                    mView.showError(e.getMessage());
                }
            }

            @Override
            public void onNext(CarDetails carDetails) {
                //mView.showDatas(taskViewData);
                mView.showViewDatas(carDetails);
            }
        });
    }

    public void ZyOperationData(String userId, String type, String Taskid, String AppraiseBackReason, String Status) {

        dataManager.ZyOperationData(userId, type, Taskid, AppraiseBackReason, Status).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Observer<SuccessData>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if (e != null) {
                    mView.showError(e.getMessage());
                }
            }

            @Override
            public void onNext(SuccessData successData) {
                mView.OperationDatas(successData);
            }
        });
    }
}
